#ifndef TOTALRES_H
#define TOTALRES_H

#include <list>
#include <QtGui/QMainWindow>
#include "ui_totalres.h"

#include "baselib\LuaScript.h"

#define MAX_RECORD_COUNT	(4)

struct TREE_RECORD
{
	int nTabFileIndex;
	int nRowIndex;
};

class TotalRes : public QMainWindow
{
	Q_OBJECT

public:
	TotalRes(QWidget *parent = 0, Qt::WFlags flags = 0);
	~TotalRes();

	static TotalRes& GetInstance(void);

	bool Init(void);
	bool UnInit(void);

	CLuaScript& GetScript(void)		{return m_Script;}

	bool RefreshDataFiles(void);
	bool RefreshTree(QNewTreeWidget* pTree, int nTabFileIndex);
	bool RefreshGroup(QComboBox* pGroup, int nTabFileIndex);
	bool RefreshEditor(QNewTreeWidget* pEditor, int nTabFileIndex, int nRowIndex);

	bool OnKeyPressEvent(QKeyEvent* event);

	virtual void keyPressEvent(QKeyEvent* event);
	virtual void showEvent(QShowEvent * event);

public slots:
	void OnSelectDataFile(bool bChecked);
	void OnTreeCurItemChanged(QTreeWidgetItem* pCurItem, QTreeWidgetItem* pPrevItem);
	void OnFocusChanged(QWidget* pOld, QWidget* pNow);
	void OnItemChanged(QTreeWidgetItem* pItem, int nColumn);
	void OnComboBoxChanged(int nIndex);
	void OnGroupChanged(const QString& text);

	void OnAbout(void);
	void OnReload(void);
	void OnSaveAll(void);

protected:
	bool _InitTree(QNewTreeWidget* pTree);
	bool _InitEditor(QNewTreeWidget* pEditor);
	bool _InitGroup(QComboBox* pGroup, QNewTreeWidget* pTree);

	bool _LinkItem(QNewTreeWidget* pFromTree, QNewTreeWidget* pFromEditor, QNewTreeWidget* pToTree, QComboBox* pToGroup);
	QString _GetLinkItemDisplayName(int nFromTabFileIndex, int nFromRowIndex, int nFromColIndex);
	bool _SearchItem(QNewTreeWidget* pTree, const char* pcszKey);
	bool _AppendRow(QNewTreeWidget* pTree);
	bool _DeleteRow(QNewTreeWidget* pTree);
	bool _CopyTree2Editor(QNewTreeWidget* pTree, QNewTreeWidget* pEditor);
	bool _CopyEditor2Editor(QNewTreeWidget* pTree, QNewTreeWidget* pEditor);

	bool _PushRecord(int nRecordIndex, int nTabFileIndex, int nRowIndex);
	const TREE_RECORD* _GetNextRecord(int nRecordIndex);
	const TREE_RECORD* _GetPrevRecord(int nRecordIndex);

	QString _GetSearchString(const char* pcszString);
private:
	typedef std::list<TREE_RECORD>	RECORD_LIST;
	RECORD_LIST				m_lstRecord[MAX_RECORD_COUNT];
	RECORD_LIST::iterator	m_itCurrentRecord[MAX_RECORD_COUNT];

	static TotalRes* m_pInstance;
	Ui::TotalResClass ui;

	QTreeWidget*	m_pLeftWidget;
	QTreeWidget*	m_pRightWidget;
	CLuaScript		m_Script;
	unsigned int	m_uLastKeyPressTime;
	char			m_szSearchKey[256];
};

#endif // TOTALRES_H
